<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-input "><label class="iconfont icon-sousuo" />
			<input type="text" confirm-type="search" v-model="search" class="font-size30" placeholder="搜索" />
		</view>
		<view v-if="systemList.length">
			<scroll-view class="content" scroll-y @scrolltolower="scrollButtonClick" id="height" :style="'height:' + height + 'px ;'">
				<!-- 列表页 -->
				<view class="uni-list-cell uni-list-cell-last" hover-class="uni-list-cell-hover" v-for="(item,index) in systemList"
				 :key="index" @tap="systemdetail(item)">
					<view class="cell-navigate">
						<view class="uni-list-data background-color-339AFF" v-model="item.F_IsRead">
							<label class="iconfont icon-tongzhi icon-label-lin" />
						</view>
						<view class="uni-item-left">
							<view class="overflow-text font-size30 color-3A3A3A"><text class="redDot" v-if="item.F_IsRead==0"></text>{{item.F_Title}}</view>
							<view class="notify-user-time">
								<view class="color-9A9A9A font-size24">{{item.F_CreatorUserId}}</view>
								<view class="color-9A9A9A font-size24">{{item.F_CreatorTime}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view v-else class="noContent">
			<img :src="nodata" alt="">
			<view>暂无数据</view>
		</view>
	</view>
</template>

<script>
	import jnpf from '@/common/js/jnpf.js'
	import helper from '@/common/js/helper.js'
	import config from '@/common/js/config.js'
	export default {
		data() {
			return {
				rows: 100,
				page: 1,
				total: 1,
				height: 0,
				systems: [],
				search: '',
				nodata: config.Resources.message.nodata,
				height1: "",
				isNoData: false,
			}
		},
		computed: {
			systemList: function() {
				let search = this.search;
				if (search) {
					return this.systems.filter(function(product) {
						return Object.keys(product).some(function(key) {
							return String(product[key]).toLowerCase().indexOf(search) > -1
						})
					})
				}
				return this.systems;
			},
			noticeMsg: function() {
				return this.$store.state.wschat.noticeMsgDef
			}
		},
		watch: {
			noticeMsg() {}
		},
		
		
		methods: {
			/* 页面初始请求 */
			init() {
				helper.ajax({
					url: '/api/system/Message',//系统消息
					method: 'GET',
					data: {
						pageSize: this.rows, //每页行数
						currentPage: this.page, //当前页
						type: "2"
					},
					success: res => {
						let _total = Math.ceil(res.data.data.pagination.total/res.data.data.pagination.pageSize)
						this.total = _total;
						let system = res.data.data.list;
						// console.log(system)
						this.isNoData = system.length > 0 ? false : true;
						for (let i = 0; i < system.length; i++) {
							this.systems.push({
								'F_Id': system[i].id,
								'F_Title': system[i].title,
								'F_CreatorUserId': system[i].creatorUser,
								'F_CreatorTime': helper.toDate(system[i].creatorTime, 'yyyy-MM-dd HH:mm'),
								'F_IsRead': system[i].isRead,
								'F_Type': system[i].type
							})
							if(system[i].isRead == 0){
								// console.log(system[i].isRead)
							}
						}
			
					}
				});
			},
			
			
			/* 列表点击事件 */
			systemdetail(e) {
				let _system = this.systems;
				let _data = helper.jsonFind(_system, function(v) {
					return v.F_Id == e.F_Id
				});
				if(e.F_IsRead == 0){
					if (_data.length > 0) {
						e.F_IsRead = 1;
					}
					const badge = uni.getStorageSync(config.Global.cache + '_Badge');
					let badgen = badge - 1;
					uni.setStorage({
						key: config.Global.cache + '_Badge',
						data: badgen
					});
					if(e.F_Type != 1){
						this.systemJump(e)
						return;
					}else{
						uni.navigateTo({
							url:  url+ e.F_Id,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}
				}else{
					if(e.F_Type != 1){
						this.systemJump(e)
						return;
					}else{
						this.noticeMsg.messageCount = this.noticeMsg.messageCount - 1;
						uni.navigateTo({
							url:  url+ e.F_Id,
							success: res => {},
							fail: () => {},
							complete: () => {}
						});
					}
				}
				
				
				
				
				
				
				
				// const badge = uni.getStorageSync(config.Global.cache + '_Badge');
				// let badgen = badge - 1;
				// uni.setStorage({
				// 	key: config.Global.cache + '_Badge',
				// 	data: badgen
				// });
				// let url = '../systemdetail/systemdetail?systemid=';
				// // console.log(e)
				// if(e.F_Type != 1){
				// 	this.systemJump(e)
				// 	return;
				// }else{
				// 	this.noticeMsg.messageCount = this.noticeMsg.messageCount - 1;
				// 	let _system = this.systems;
				// 	let _data = helper.jsonFind(_system, function(v) {
				// 		return v.F_Id == e.F_Id
				// 	});
				// 	if (_data.length > 0) {
				// 		e.F_IsRead = 1;
				// 	}
					
				// 	uni.navigateTo({
				// 		url:  url+ e.F_Id,
				// 		success: res => {},
				// 		fail: () => {},
				// 		complete: () => {}
				// 	});
				// }
			},
			
			/* 系统消息列表跳转 */
			async systemJump(e){
				let url;
				const res = await helper.ajaxPromise({
					url: '/api/system/Message/ReadInfo/'+e.F_Id,
				})
				// console.log(res)
				let bodyText = res.data.data.bodyText
				if(bodyText.indexOf('流')>=0 || bodyText == ''){
					// console.log("旧数据")
				}else{
					bodyText = JSON.parse(res.data.data.bodyText)
				}
				
				
				// console.log(bodyText.type)
				switch(bodyText.type){
					case 1:
						url = '../../../../work/operate/started/started'
					break;
					case 2:
						url = '../../../../work/operate/backlog/backlog'
					break;
					case 3:
						url = '../../../../work/operate/alreadybacklog/alreadybacklog'
					break;
					 default: 
                        url = "../../../../work/operate/started/started";	
				}
				
				
				
				uni.navigateTo({
					url:  url,
					success: ress => {
						// console.log(ress)
						// url = ''
					},
					fail: (err) => {
						// console.log(err.errMsg)
					},
					complete: () => {}
				});
			},
			
			
			/* 滚动底部加载 */
			scrollButtonClick(e) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				let _this = this;
				if (_this.total > _this.page) {
					_this.page += 1;
					//触发求
					helper.ajax({
						url: '/api/system/Message',//系统消息
						method: 'GET',
						data: {
							pageSize: _this.rows, //每页行数
							currentPage: _this.page, //当前页
							type: "2"
						},
						success: res => {
							let _total = Math.ceil(res.data.data.pagination.total/res.data.data.pagination.pageSize)
							this.total = _total;
							let system = res.data.data.list;
							for (let i = 0; i < system.length; i++) {
								this.systems.push({
									'F_Id': system[i].id,
									'F_Title': system[i].title,
									'F_CreatorUserId': system[i].creatorUser,
									'F_CreatorTime': helper.toDate(system[i].creatorTime, 'yyyy-MM-dd HH:mm:ss'),
									'F_IsRead': system[i].isRead,
									'F_Type': system[i].type
								})
							}
						}
					});
				}
				uni.hideLoading();
			},
			
		},
		
		
		
		
		mounted() {
			let _this = this;
			// 获取设备宽度
			uni.getSystemInfo({
				success: function(res) {
					_this.height = res.windowHeight;
				}
			});
			if (this.systemList.length > 0) {
				let search = uni.createSelectorQuery().select(".search-input");
				search.boundingClientRect(data => {
					_this.rows = Math.ceil((_this.height - data.height) / 58)
				}).exec();
			}
			//初始化数据
			_this.init();
		},
	}
	
</script>

<style lang="scss">
	page {
		background-color: #f0f2f6
	}

	.overflow-text {
		width: 99%;
		display: flex;
		align-items: center;
	}

	.uni-list-data {
		width: 66upx;
		height: 66upx;
	}

	.search-input {
		background-color: #fff;
	}

	.uni-list-cell {
		background-color: #fff;

		&:last-child {
			.cell-navigate::after {
				height: 0;
			}
		}
	}
</style>
